package com.example.springbolgdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbolgdemo.common.exception.BlogException;
import com.example.springbolgdemo.common.util.BeanTransformUtils;
import com.example.springbolgdemo.common.util.JwtUtils;
import com.example.springbolgdemo.common.util.SecurityUtils;
import com.example.springbolgdemo.contstant.Contstants;
import com.example.springbolgdemo.mapper.BlogInfoMapper;
import com.example.springbolgdemo.mapper.UserInfoMapper;
import com.example.springbolgdemo.pojo.dataobject.BlogInfo;
import com.example.springbolgdemo.pojo.dataobject.UserInfo;
import com.example.springbolgdemo.pojo.request.UserLoginRequest;
import com.example.springbolgdemo.pojo.response.UserInfoResponse;
import com.example.springbolgdemo.pojo.response.UserLoginResponse;
import com.example.springbolgdemo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        UserInfo userInfo=queryUserInfoByName(userLoginRequest.getUserName());
        if (userInfo==null||userInfo.getId()<=0){
            throw new BlogException("用户不存在");
        }
        //判断密码是否正确
//        if (!userLoginRequest.getPassword().equals(userInfo.getPassword())){
//            throw new BlogException("用户密码错误");
//        }
        //对加密密码进行校验
        if (!SecurityUtils.verify(userLoginRequest.getPassword(),userInfo.getPassword())){
            throw new BlogException("用户密码错误");
        }
        //密码正确
        Map<String,Object> claims=new HashMap<>();
        claims.put("id",userInfo.getId());
        claims.put("userName",userInfo.getUserName());

        //生成响应对象并返回
        return new UserLoginResponse(userInfo.getId(), JwtUtils.genToken(claims));
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfo userInfo=getUserInfoById(userId);
        return BeanTransformUtils.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        //获取作者id
        BlogInfo blogInfo=getBlogInfo(blogId);
        if (blogInfo==null||blogInfo.getUserId()<=0){
            throw new BlogException("blogId不合法");
        }
        //根据作者id,获取作者信息
        UserInfo userInfo=getUserInfoById(blogInfo.getUserId());
        return BeanTransformUtils.trans(userInfo);
    }

    /**
     * 通过id查询userInfo
     * @param userId
     * @return
     */
    public UserInfo getUserInfoById(Integer userId){
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getId,userId)
                .eq(UserInfo::getDeleteFlag,Contstants.NOT_DELETE));
    }

    /**
     * 根据用户名称查询用户信息
     * @param userName
     * @return
     */
    public UserInfo queryUserInfoByName(String userName){
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getUserName,userName).eq(UserInfo::getDeleteFlag, Contstants.NOT_DELETE);
        return userInfoMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 根据blogId查询博客信息
     * @param blogInfoId
     * @return
     */
    public BlogInfo getBlogInfo(Integer blogInfoId){
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId,blogInfoId)
                .eq(BlogInfo::getDeleteFlag,Contstants.NOT_DELETE));
    }

}
